<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts/lib/echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    yaochuan:"hello",
    pie:{
      className: {
        type: String,
        default: 'chart'
      },
      data:{
        type: Object,
        default: [
                  {value: 70, name: '汽油'},
                  {value: 30, name: '柴油'},
              ]
      },
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      yaochuan:"gaga"
    }
  },
  mounted() {
    window.console.log("mounted"+this.pie.data);
    this.$nextTick(() => {
      this.initChart()
    });
    setTimeout(() => {
      this.chart.resize();
    }, 2000);  
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch(){
      window.console.log("watch"+this.yaochuan);
  },
  methods: {
    initChart() {
       window.console.log("init"+this.yaochuan);
      this.chart = echarts.init(this.$el, 'macarons')
      
      this.chart.setOption({
          title: {
              text: this.pie.className,
          },
          tooltip: {
              trigger: 'item'
          },
          legend: {
              top: '0%',
              left: 'right'
          },
          series: [
              {
                  name: '',
                  type: 'pie',
                  radius: ['40%', '70%'],
                  avoidLabelOverlap: false,
                  itemStyle: {
                      borderRadius: 10,
                      borderColor: '#fff',
                      borderWidth: 2
                  },
                  label: {
                      show: false,
                      position: 'center'
                  },
                  emphasis: {
                      label: {
                          show: true,
                          fontSize: '40',
                          fontWeight: 'bold'
                      }
                  },
                  labelLine: {
                      show: false
                  },
                  data: this.pie.data
              }
          ]
      })
          }
        }
      }
</script>
